#import "BWCenteringClipView.h"

@implementation BWCenteringClipView

- (id) initWithScrollView: (NSScrollView *) scrollview
{
    NSRect scrollFrame = [[scrollview contentView] frame];

    if (self = [super initWithFrame: scrollFrame]) {
        NSClipView *oldClip;
        oldClip = [scrollview contentView];

        [self setDocumentView: [oldClip documentView]];
        [scrollview setContentView: self];
        
        // otherwise old copies of the view get splatted around
        [scrollview setDrawsBackground: NO];

        // fill in the background with the fill color
        [self setDrawsBackground: YES];
    }

    return (self);

} // initWithScrollView


- (void) centerDocument
{
    NSRect documentRect = [[self documentView] frame];
    NSRect clipRect = [self bounds];

    if (documentRect.size.width < clipRect.size.width) {
        clipRect.origin.x =
            roundf ((documentRect.size.width - clipRect.size.width) / 2.0);
    }

    if (documentRect.size.height < clipRect.size.height) {
        clipRect.origin.y = 
            roundf ((documentRect.size.height - clipRect.size.height) / 2.0);
    }

    [self scrollToPoint: clipRect.origin];

} // centerDocument



// override this so the superclass doesn't override the new origin point.

- (NSPoint) constrainScrollPoint: (NSPoint) proposedNewOrigin
{
    NSRect documentRect = [[self documentView] frame];
    NSRect clipRect = [self bounds];

    NSPoint newScrollPoint = proposedNewOrigin;

    float maxX = documentRect.size.width - clipRect.size.width;
    float maxY = documentRect.size.height - clipRect.size.height;

    // if the clip view is wider than the document, can't scroll horizontally
    if (documentRect.size.width < clipRect.size.width) {
        newScrollPoint.x = roundf (maxX / 2.0);
    } else {
        newScrollPoint.x = roundf (MAX(0,MIN (newScrollPoint.x,maxX)));
    }

    // if the clip view is taller than the document, can't scroll vertically
    if (documentRect.size.height < clipRect.size.height) {
        newScrollPoint.y = roundf (maxY / 2.0);
    } else {
        newScrollPoint.y = roundf (MAX(0,MIN (newScrollPoint.y,maxY)));
    }

    return (newScrollPoint);

} // constrainScrollPoint


// ----------------------------------------
// just the existance of these methods being overridden causes
// things to kinda sorta work.  weird.

- (void) viewBoundsChanged: (NSNotification *) notification
{
    [super viewBoundsChanged: notification];

} // viewBoundsChanged


- (void) viewFrameChanged: (NSNotification *) notification
{
    [super viewFrameChanged: notification];

} // viewFrameChanged


- (void)setFrame:(NSRect)frameRect
{
    [super setFrame:frameRect];
    [self centerDocument];
}

- (void)setFrameOrigin:(NSPoint)newOrigin
{
    [super setFrameOrigin:newOrigin];
    [self centerDocument];
}

- (void)setFrameSize:(NSSize)newSize
{
    [super setFrameSize:newSize];
    [self centerDocument];
}

- (void)setFrameRotation:(float)angle
{
    [super setFrameRotation:angle];
    [self centerDocument];
}

@end  // BWCenteringClipView


